package com.juli.aimanager;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 工具类
 * @author 小帅丶
 * @类名称  Global
 * @remark 
 * @date  2017-7-13
 */
public class Global {
	public static boolean isEmpty(String str) {
		return str == null || str.equals("") || str.equals("undefined") ? true
				: false;
	}

	// public static final String PROGRAM = "0908030001";

	public static String getSysDateDay() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	/**
	 * 
	 * @param timeDifference
	 *            时间差,返回前系统时间的加上timeDifference天后的时间,为负值代表之前几天;
	 * @return
	 */
	public static String getSysDateDay(Long timeDifference) {
		Calendar cl = Calendar.getInstance();
		Long clTemp = cl.getTimeInMillis()
				+ Long.valueOf((timeDifference * 24 * 60 * 60 * 1000));
		cl.setTimeInMillis(clTemp);
		Date date = cl.getTime();
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
		return myFmt.format(date.getTime());

	}

	/**
	 * 
	 * @param timeDifference
	 *            根据日期和天数得出相加之后的天数;
	 * @return
	 */
	public static String getDate(Date date, int day) {
		Long clTemp = date.getTime()
				+ Long.valueOf((day * 24 * 60 * 60 * 1000));
		Date date1 = new Date(clTemp);
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
		return myFmt.format(date1.getTime());

	}

	public static String getSysDateTime(Long timeDifference) {
		Calendar cl = Calendar.getInstance();
		Long clTemp = cl.getTimeInMillis()
				+ Long.valueOf((timeDifference * 24 * 60 * 60 * 1000));
		cl.setTimeInMillis(clTemp);
		Date date = cl.getTime();
		SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return myFmt.format(date.getTime());

	}

	public static Date getSysDateDayToDate(Long timeDifference) {
		Calendar cl = Calendar.getInstance();
		Long clTemp = cl.getTimeInMillis()
				+ Long.valueOf((timeDifference * 24 * 60 * 60 * 1000));
		cl.setTimeInMillis(clTemp);
		Date date = cl.getTime();

		return date;

	}

	public static Date getSysDateDayToDate() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		java.util.Date currDate = new java.util.Date();

		return currDate;

	}

	/**
	 * 2010-1-17
	 * 
	 * @param String
	 *            startDay 需要比较的时间 不能为空(null),需要正确的日期格式 ,如：2009-09-12
	 * @param String
	 *            endDay 被比较的时间 为空(null)则为当前时间
	 * @param int stype 返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return 举例： compareDate("2009-09-12", null, 0);//比较天
	 *         compareDate("2009-09-12", null, 1);//比较月
	 *         compareDate("2009-09-12", null, 2);//比较年
	 */
	public static int compareDate(String startDay, String endDay, int stype) {
		int n = 0;
		String[] u = { "天", "月", "年" };
		String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";

		endDay = endDay == null ? getCurrentDate("yyyy-MM-dd") : endDay;

		DateFormat df = new SimpleDateFormat(formatStyle);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(startDay));
			c2.setTime(df.parse(endDay));
		} catch (Exception e3) {
			System.out.println("wrong occured");
		}
		while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果
			n++;
			if (stype == 1) {
				c1.add(Calendar.MONTH, 1); // 比较月份，月份+1
			} else {
				c1.add(Calendar.DATE, 1); // 比较天数，日期+1
			}
		}
		n = n - 1;
		if (stype == 2) {
			n = (int) n / 365;
		}
		return n;
	}

	/**
	 * 2010-1-17
	 * 
	 * @param Date
	 *            startDate 需要比较的时间 不能为空(null),需要正确的日期格式 ,如：2009-09-12
	 * @param Date
	 *            endDate 被比较的时间 为空(null)则为当前时间
	 * @param int stype 返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return 举例： compareDate(2009-09-12, null, 0);//比较天
	 *         compareDate(2009-09-12, null, 1);//比较月 compareDate(2009-09-12,
	 *         null, 2);//比较年
	 */
	public static int compareDate(Date startDate, Date endDate, int stype) {
		String startDay = FormatDate(startDate, "yyyy-MM-dd HH:mm:ss");
		String endDay = FormatDate(endDate, "yyyy-MM-dd HH:mm:ss");
		return compareDate(startDay, endDay, stype);
	}

	public static String getCurrentDate(String format) {
		Calendar day = Calendar.getInstance();
		day.add(Calendar.DATE, 0);
		SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
		String date = sdf.format(day.getTime());
		return date;
	}

	public static String getSysDateYear() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String getSysDateYearAndMonth() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String FormatDate(Date date1, String format) {
		if (format == null || "".equals(format)) {
			format = "yyyy-MM-dd";
		}
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				format);
		String curDate = formatter.format(date1);
		return curDate;

	}

	public static String FormatDate(Date date1) {

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String curDate = formatter.format(date1);
		return curDate;

	}

	public static String FormatDateYYYYMMDD(Date date1) {

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyyMMdd");
		String curDate = formatter.format(date1);
		return curDate;

	}

	public static String FormatDateWithHOUR(Date date1) {

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH");
		String curDate = formatter.format(date1);
		return curDate;

	}

	public static String FormatDateWithHOURAndMinute(Date date1) {

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm");
		String curDate = formatter.format(date1);
		return curDate;

	}

	public static Date StringToDate(String source, String frm) {
		SimpleDateFormat sdf = new SimpleDateFormat(frm);
		Date tdate = null;
		try {
			tdate = sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tdate;
	}

	public static Date StringToDate(String source) {
		String frm = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(frm);
		Date tdate = null;
		try {
			tdate = sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tdate;
	}

	public static String getSysDateDayTime() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String getSysTime() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"HH:mm:ss");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String getSysTimeyyyyMMddhhmmssSSS() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyyMMddhhmmssSSS");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String getRequiredTime(Date date) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"HH:mm:ss");
		String curDate = formatter.format(date);
		return curDate;

	}

	public static String getRequiredTimeHY() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"HH:mm:ss");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate.replace(":", "-");
	}

	public static String getRequiredFormatTime(Date date) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String curDate = formatter.format(date);
		return curDate;

	}

	public static String getRequiredFormatTimeYear(Date date) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy");
		String curDate = formatter.format(date);
		return curDate;

	}

	public static String getMonthOfSysTime() {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"MM");
		java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(currDate);
		return curDate;

	}

	public static String getDateDayTime2(Date date) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		// java.util.Date currDate = new java.util.Date();
		String curDate = formatter.format(date);
		return curDate;

	}

	// 计算n月以后的日期
	public static Date getIncMonthDate(Date date, int months) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, months);
		Date newDate = c.getTime();
		return newDate;
	}

	// 计算n天以后的日期
	public static Date getAfterDate(Date date, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		Date newDate = c.getTime();
		return newDate;
	}

	// 计算n天以后的日期
	public static String getAfterDateStr(Date date, int days) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days);
		String newDate = formatter.format(c.getTime());
		return newDate;
	}

	// 计算n天以后的日期
	public static String getAfterDateStr(String date, int days) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(StringToDate(date));
		c.add(Calendar.DATE, days);
		String newDate = formatter.format(c.getTime());
		return newDate;
	}

	// 计算n小时以后的日期
	public static Date getIncHourDate(Date date, int hours) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, hours);
		Date newDate = c.getTime();
		return newDate;
	}

	// 计算两个日期之间相差的天数
	public static int getDiffDays(Date beginDate, Date endDate) {
		long lBeginTime = beginDate.getTime();
		long lEndTime = endDate.getTime();
		int iDay = (int) ((lEndTime - lBeginTime) / 86400000);
		return iDay;
	}

	// 判断是否为数字
	public static boolean isNumeric(String str) {
		try {
			Double num = Double.valueOf(str);// 把字符串强制转换为数字
			return true;// 如果是数字，返回True
		} catch (Exception e) {
			return false;// 如果抛出异常，返回False
		}
	}

	// 获取当月第一天
	public static String getFirstDayOfMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 计算当月最后一天,返回字符串
	public static String getLastDayOfMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		lastDate.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天

		str = sdf.format(lastDate.getTime());
		return str;
	}
}
